package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZLJ
 * @date 2025/10/8 13:35
 * @Description
 */
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/list")
    public Result selectAll(){
        List<Role> roles = roleService.selectAll();
        return Result.success(roles);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        log.info("删除角色id:{}",id);
        roleService.deleteById(id);
        return Result.success();
    }
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("修改角色:{}",role);
        roleService.update(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        log.info("查询角色id:{}",id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @GetMapping
    public Result page(
            @RequestParam(name="name",required = false) String name,
            @RequestParam(name="page",required = true,defaultValue = "1") Integer page,
            @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize){

        PageResult<Role> pageResult = roleService.page(name,page,pageSize);
        log.info("分页查询角色,参数:角色名称={},当前页码={},每页大小={}", name, page, pageSize);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("新增角色:{}",role);
        roleService.addRole(role);
        return Result.success();
    }
}
